package com.plugins.usereflect;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

//文本文件内容替换,源码批量替换
public class A01_REPSTR_IN implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= "";
	//包含文件路径
	public String			param_value1	= null;
	//剔除文件路径
	public String			param_value2	= null;
	//源文件内容
	public String			param_value3	= null;
	//倒序删除行数
	public String			param_value4	= null;
	//替换新行内容
	public String			param_value5	= null;
	//特殊独立文件
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;
	
	//包含文件路径
	public List<String> includeDir = new ArrayList<String>();
	//单独包含文件
	public List<String> includeFile = new ArrayList<String>();
	//过滤文件路径
	public List<String> noincludeDir = new ArrayList<String>();
	//文件替换内容,前推N行内容
	public List<String> repOldAtt = new ArrayList<String>();
	public int iLine = -1;
	
	//文件替换内容,某行替换新内容
	public String repNewLine = "";
	public String attRDA = "XXXDDD";
	public String attTableName = "DDDXXX";
	//匹配之后往前删除行数
	public int RemoveLine = 0;
	
	public void init_start()
	{
//		String[] str1 = this.param_value1.split("\\\\r\\\\n");
//		String[] str2 = this.param_value1.split("\\r\\n");
//		String[] str3 = this.param_value1.split("\r\n");
//		String[] str4 = this.param_value1.split("\\\\r");
//		String[] str5 = this.param_value1.split("\\\r");
//		String[] str6 = this.param_value1.split("\\r");
//		String[] str7 = this.param_value1.split("\r");
//		System.out.print("str1=>"+str1.length+";str2=>"+str2.length+";str3=>"+str3.length);
//		System.out.print("str5=>"+str5.length+";str6=>"+str6.length+";str7=>"+str7.length);
		
		for(String strTem : this.param_value1.split("\\n")){
			includeDir.add(strTem);
		}
		for(String strTem : this.param_value2.split("\\n")){
			noincludeDir.add(strTem);
		}
		for(String strTem : this.param_value6.split("\\n")){
			includeFile.add(strTem);
		}
		for(String strTem : this.param_value3.split("\\n")){
			repOldAtt.add(strTem);
		}
		this.repNewLine = this.param_value5;
		this.RemoveLine = Integer.valueOf(this.param_value4);
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp");

//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\js");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\css");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\fonts");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\img");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\META-INF");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CenterData\\webapp\\WEB-INF");
		
//		repOldAtt.add("/*绑定show监听事件*/");
//		repOldAtt.add("if({$RDA$}_Tab_Flag == \"-1\"){");
//		repOldAtt.add("	var {$RDA$}_n = Get_RandomDiv(\"{$RDA$}\",\"\");");
//		repOldAtt.add("	$({$RDA$}_n).bind(\"show\", function(objTag){");
//		repOldAtt.add("		{$RDA$}_Adjust_Sub_Sequ();");
//		repOldAtt.add("		if({$RDA$}_Tab_Flag > 0 && objTag.currentTarget.id.toString().indexOf({$RDA$}_n+\"_div\") == 0)");
//		RemoveLine = 0;
//		repNewLine = "		if({$RDA$}_Tab_Flag > 0 && (\"#\"+objTag.currentTarget.id.toString()) == {$RDA$}_n)";
		
//		repOldAtt.add("		onPostBody:function(){");
//		repOldAtt.add("		}");
//		repOldAtt.add("}");
//		repNewLine = "		AD1D02_page_end();\r\n}";
			
//		repOldAtt.add("	var s_data = input.N01_sel_{template_table_EN};");
//		repOldAtt.add("	if(s_data.length > 0)");
//		repOldAtt.add("		{$RDA$}_rowCheckData = s_data[0];");
//		repOldAtt.add("	{$RDA$}_select_{template_table_EN}_rowId = \"\";");
//		repOldAtt.add("	{$RDA$}_Tab_Flag = 0;");
//		RemoveLine = 1;
//		repNewLine = "";
		
//		repOldAtt.add("	onPostBody:function(){");
//		repOldAtt.add("		if(s_data.length != 0)");
//		repOldAtt.add("			{$RDA$}_page_end();");
//		RemoveLine = 2;
//		repNewLine = "";
		
//		repOldAtt.add("		eval(inputrandom+\"_param = {}\");");
//		repOldAtt.add("			if(rowData != null){");
//		repOldAtt.add("				var temFlag = true;");
//		repOldAtt.add("				$.each(temPar, function (i, obj) {");
//		repOldAtt.add("					if(eval(inputrandom+\"_param.hasOwnProperty(\\\"\"+obj.target_id+\"\\\")\") && eval(inputrandom+\"_param[\\\"\"+obj.target_id+\"\\\"]\").toString() == rowData[obj.sourc_id].toString())");
//		repOldAtt.add("						temFlag = false;");
//		repOldAtt.add("					eval(inputrandom+\"_param[\\\"\"+obj.target_id+\"\\\"] = \\\"\"+rowData[obj.sourc_id]+\"\\\"\");");
//		RemoveLine = 1;
//		repNewLine = "				else\r\n					eval(inputrandom+\"_param[\\\"\"+obj.target_id+\"\\\"] = \\\"\"+rowData[obj.sourc_id]+\"\\\"\");";
			
//		repOldAtt.add("/*绑定show监听事件*/");
//		repOldAtt.add("if({$RDA$}_Tab_Flag == \"-1\"){");
//		repOldAtt.add("	var n = Get_RandomDiv(\"{$RDA$}\",\"\");");
//		repOldAtt.add("	$(n).bind(\"show\", function(objTag){");
//		repOldAtt.add("		{$RDA$}_Adjust_Sub_Sequ();");
//		repOldAtt.add("		if({$RDA$}_Tab_Flag > 0)");
//		RemoveLine = 1;
//		repNewLine = "		if({$RDA$}_Tab_Flag > 0 && (\"#\"+objTag.currentTarget.id.toString()) == n)";
	
//		repOldAtt.add("		onPostBody:function(){");
//		repOldAtt.add("		}");
//		repOldAtt.add("	});");
//		repOldAtt.add("{$RDA$}_page_end();");
//		RemoveLine = 1;
//		repNewLine = "	{$RDA$}_page_end();";
    
//		repOldAtt.add("		icons: {");
//		repOldAtt.add("			refresh: \"glyphicon-repeat\",");
//		repOldAtt.add("			toggle: \"glyphicon-list-alt\",");
//		repOldAtt.add("			columns: \"glyphicon-list\"");
//		repOldAtt.add("		}");
//		RemoveLine = 1;
//		repNewLine = "		},";
		
//		repOldAtt.add("/*绑定show监听事件*/");
//		repOldAtt.add("if(A40199_Tab_Flag == \"-1\"){");
//		repOldAtt.add("	var n = Get_RandomDiv(\"A40199\",\"\");");
//		repOldAtt.add("	$(n).bind(\"show\", function(objTag){");
//		repOldAtt.add("		A40199_Adjust_Sub_Sequ();");
//		repOldAtt.add("		if(A40199_Tab_Flag > 0 && (\"#\"+objTag.currentTarget.id.toString()) == n)");
//		RemoveLine = 4;
//		repNewLine = "	var {$RDA$}_n = Get_RandomDiv(\"{$RDA$}\",\"\");\r\n";
//		repNewLine +="	$({$RDA$}_n).bind(\"show\", function(objTag){\r\n";
//		repNewLine +="		{$RDA$}_Adjust_Sub_Sequ();\r\n";
//		repNewLine +="		if({$RDA$}_Tab_Flag > 0 && (\"#\"+objTag.currentTarget.id.toString()) == {$RDA$}_n)";
		
		
		// 线程异步调用
		Runnable Thread_A01_REPSTR_IN = new Runnable(){
			@Override
			public void run(){
				System.out.println("==>遍历所有源码文件并替换内容开始:"+Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss"));
				
				//文件夹
				for(String fileDir : includeDir){
					FindDir(fileDir);
				}
				
				//文件
				for(String fileName : includeFile){
					if(ReadFile(fileName)){
						File fiFile = new File(fileName);
						SaveReplaceFile(fiFile);
					}
				}
				System.out.println("==>遍历所有源码文件并替换内容完成:"+Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss"));
			}
			
			//递归查找文件或文件夹
			public void FindDir(String fileDir){
				//是否过滤文件夹
				for(String noDir : noincludeDir){
					if(java.util.Objects.equals(noDir,fileDir))
						return;
				}
				File listfile = new File(fileDir);
				if(listfile.exists() || listfile.isDirectory()){
		            for(File file : listfile.listFiles()){
		                if(file.isDirectory()){
		                	FindDir(file.getAbsolutePath());
		                }else if(ReadFile(file.getAbsolutePath())){
		                	SaveReplaceFile(file);
		                }
		            }
	            }
			}
			
			//查找内容行前推N行
			public boolean FindLinePre(String strLine){
				if(iLine == repOldAtt.size() - 1)
					iLine = -1;
				if(java.util.Objects.equals(strLine.trim(),repOldAtt.get(iLine + 1).replace("{$RDA$}",attRDA).replace("{template_table_EN}",attTableName).trim())){
					iLine ++;
					return true;
				}else{
					iLine = -1;
					return false;
				}
			}
			
			//替换文件中内容
			public boolean SaveReplaceFile(File fiFile){
				boolean bResult = false;
				String initFileName = "";
				String strPath = "";
				String randomName = "";
				iLine = -1;
				//新文件内容
				List<String> newFileCont = new ArrayList<String>(); 
				if(!fiFile.exists()){
					return bResult;
				}else
				{			
					File NewfiFile  = null;		
					FileInputStream fr = null;
					InputStreamReader is = null;
					BufferedReader br = null;
					FileWriter sw = null;
					try
					{
						fr = new FileInputStream(fiFile.getAbsolutePath());
						is = new InputStreamReader(fr,Com_Para.U_CODE);
						br = new BufferedReader(is);
						while(true)
						{
							String line = br.readLine();
							if(java.util.Objects.equals(line,null))
								break;
							newFileCont.add(line);
							if(FindLinePre(line) && iLine == repOldAtt.size() - 1){
								for(int nn = 1; nn <= RemoveLine; nn ++)
									newFileCont.remove(newFileCont.size() - 1);
								if(!java.util.Objects.equals(repNewLine,""))
									newFileCont.add(repNewLine.replace("{$RDA$}",attRDA).replace("{template_table_EN}",attTableName));
							}
						}
						initFileName = fiFile.getName();
						strPath = fiFile.getParentFile().getAbsolutePath();
						
						randomName = strPath+Com_Para.zxyPath+Com_Fun.Get_New_GUID().replace("-","")+".js";
						NewfiFile = new File(randomName);
						NewfiFile.createNewFile();
						sw = new FileWriter(randomName,true);
						for(String strContent : newFileCont){
							if(!java.util.Objects.equals(strContent,null))
								sw.write(strContent.toString()+"\r\n");
						}
					}
					catch(Exception ex)
					{
						ex.printStackTrace();
					}
					finally
					{
						try
						{
							sw.close();
							is.close();
							br.close();
							fr.close();
							boolean bDel = fiFile.delete();
							boolean bRe = NewfiFile.renameTo(new File(strPath+Com_Para.zxyPath+initFileName));
						}
						catch(Exception ex)
						{
						}
					}
					
					return bResult;
				}
			}
			
			//读取文件
			public boolean ReadFile(String FileName){
				boolean bResult = false;
				attRDA = "XXXDDD";
				attTableName = "DDDXXX";
				iLine = -1;
				
				File fiFile = new File(FileName);
				FileInputStream fr = null;
				if(!fiFile.exists()){
					return bResult;
				}else
				{
					if(FileName.lastIndexOf("$.js") == FileName.length() - 4)
						return bResult;
					else if(FileName.lastIndexOf(".js") != FileName.length() - 3)
						return bResult;
					
					InputStreamReader is = null;
					BufferedReader br = null;
					try
					{
						attTableName = fiFile.getName().substring(0,fiFile.getName().length() - 3);
						fr = new FileInputStream(FileName);
						is = new InputStreamReader(fr,Com_Para.U_CODE);
						br = new BufferedReader(is);
						int iIndex = 0; 
						while(true)
						{
							String line = br.readLine();
							if(java.util.Objects.equals(line,null))
								break;
							if(FindLinePre(line) && iLine == repOldAtt.size() - 1){
								bResult = true;
								break;
							}
							if(iIndex == 1 && line.length() > 10){
								attRDA = line.substring(4,10);
							}
							iIndex ++;
						}
					}
					catch(Exception ex)
					{
						ex.printStackTrace();
					}
					finally
					{
						try
						{
							is.close();
							br.close();
							fr.close();
						}
						catch(Exception ex)
						{
						}
					}
					
					return bResult;
				}
			}
		};

		// 异步调用
		Thread authThread = new Thread(Thread_A01_REPSTR_IN);
		authThread.start();
		authThread.setName("线程Thread_A01_POROCU_IN");
		this.strResult.delete(0,this.strResult.length());
		this.strResult.append(Com_Fun.get_ope_result(param_name,"1","文本文件内容替换,请等待"));
	}
	/**
	 * @return strResult
	 */
	public StringBuilder getStrResult()
	{
		return strResult;
	}
	/**
	 * @param strResult 要设置的 strResult
	 */
	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}
	/**
	 * @return user_main_id
	 */
	public String getUser_main_id()
	{
		return user_main_id;
	}
	/**
	 * @param user_main_id 要设置的 user_main_id
	 */
	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}
	/**
	 * @return session_id
	 */
	public String getSession_id()
	{
		return session_id;
	}
	/**
	 * @param session_id 要设置的 session_id
	 */
	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}
	/**
	 * @return param_name
	 */
	public String getParam_name()
	{
		return param_name;
	}
	/**
	 * @param param_name 要设置的 param_name
	 */
	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}
	/**
	 * @return param_value1
	 */
	public String getParam_value1()
	{
		return param_value1;
	}
	/**
	 * @param param_value1 要设置的 param_value1
	 */
	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}
	/**
	 * @return param_value2
	 */
	public String getParam_value2()
	{
		return param_value2;
	}
	/**
	 * @param param_value2 要设置的 param_value2
	 */
	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}
	/**
	 * @return param_value3
	 */
	public String getParam_value3()
	{
		return param_value3;
	}
	/**
	 * @param param_value3 要设置的 param_value3
	 */
	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}
	/**
	 * @return param_value4
	 */
	public String getParam_value4()
	{
		return param_value4;
	}
	/**
	 * @param param_value4 要设置的 param_value4
	 */
	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}
	/**
	 * @return param_value5
	 */
	public String getParam_value5()
	{
		return param_value5;
	}
	/**
	 * @param param_value5 要设置的 param_value5
	 */
	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}
	/**
	 * @return param_value6
	 */
	public String getParam_value6()
	{
		return param_value6;
	}
	/**
	 * @param param_value6 要设置的 param_value6
	 */
	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}
	/**
	 * @return param_value7
	 */
	public String getParam_value7()
	{
		return param_value7;
	}
	/**
	 * @param param_value7 要设置的 param_value7
	 */
	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}
	/**
	 * @return param_value8
	 */
	public String getParam_value8()
	{
		return param_value8;
	}
	/**
	 * @param param_value8 要设置的 param_value8
	 */
	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}
	/**
	 * @return param_value9
	 */
	public String getParam_value9()
	{
		return param_value9;
	}
	/**
	 * @param param_value9 要设置的 param_value9
	 */
	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}
	/**
	 * @return param_value10
	 */
	public String getParam_value10()
	{
		return param_value10;
	}
	/**
	 * @param param_value10 要设置的 param_value10
	 */
	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}
